﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevExpress.XtraGrid.Views.Tile;

namespace Tool_System_Client
{
    public partial class Tool_Apply_Form : DevExpress.XtraEditors.XtraForm
    {
        DataTable m_oDataTable = new DataTable();
        Dictionary<int, bool> m_dicToolSelect = new Dictionary<int, bool>();

        public Tool_Apply_Form()
        {
            InitializeComponent();

            this.WindowState = FormWindowState.Maximized;
            tileView1.OptionsBehavior.AllowSmoothScrolling = true;
        }

        private void Tool_Apply_Form_Load(object sender, EventArgs e)
        {
            InitData();
            SetupView();
        }

        protected virtual void InitData()
        {
            try
            {
                m_oDataTable = DBManager.GetInstance().GetNormalToolList();
                m_oDataTable.Columns.Add("Image", typeof(Image));
                m_oDataTable.Columns.Add("f_toolboxname", typeof(string));

                foreach (DataRow dataRow in m_oDataTable.Rows)
                {
                    int iID = int.Parse(dataRow["f_id"].ToString());
                    m_dicToolSelect[iID] = false;

                    string sImage = dataRow["f_picture"].ToString();
                    if (sImage.Length > 0)
                    {
                        try
                        {
                            var img = TS_XY_Train_Repaire.ToolSystem.Utility.ChangeStringToImage(sImage);
                            dataRow["Image"] = new Bitmap(img, new Size(190, 190));
                        }
                        catch { }
                    } 

                    int iToolbox = int.Parse(dataRow["f_toolbox"].ToString());
                    if (iToolbox != 0)
                    {
                        dataRow["f_toolboxname"] = "工具箱";
                    }
                    else
                    {
                        dataRow["f_toolboxname"] = "工具";
                    }
                }

                gridControl1.DataSource = m_oDataTable;
            }
            catch { }
        }

        void SetupView()
        {
            try
            {
                int Cap2ValAnchor = 0;
                int Item2ItemAnchor = 3;

                // Setup tiles options
                tileView1.BeginUpdate();
                tileView1.OptionsTiles.RowCount = 3;
                tileView1.OptionsTiles.Padding = new Padding(20);
                tileView1.OptionsTiles.ItemPadding = new Padding(10);
                tileView1.OptionsTiles.IndentBetweenItems = 20;
                tileView1.OptionsTiles.ItemSize = new Size(340, 190);
                tileView1.Appearance.ItemNormal.ForeColor = Color.White;
                tileView1.Appearance.ItemNormal.BorderColor = Color.Transparent;
                tileView1.ColumnSet.GroupColumn = tileView1.Columns["f_toolboxname"];
                //Setup tiles template
                TileViewItemElement leftPanel = new TileViewItemElement();
                TileViewItemElement splitLine = new TileViewItemElement();
                TileViewItemElement idCaption1 = new TileViewItemElement();
                TileViewItemElement idValue = new TileViewItemElement();
                TileViewItemElement nameCaption = new TileViewItemElement();
                TileViewItemElement nameValue = new TileViewItemElement();
                TileViewItemElement rfidCaption = new TileViewItemElement();
                TileViewItemElement rfidValue = new TileViewItemElement();
                TileViewItemElement toolbox = new TileViewItemElement();
                TileViewItemElement image = new TileViewItemElement();
                tileView1.TileTemplate.Add(leftPanel);
                tileView1.TileTemplate.Add(splitLine);
                tileView1.TileTemplate.Add(idCaption1);
                tileView1.TileTemplate.Add(idValue);
                tileView1.TileTemplate.Add(nameCaption);
                tileView1.TileTemplate.Add(nameValue);
                tileView1.TileTemplate.Add(rfidCaption);
                tileView1.TileTemplate.Add(rfidValue);
                tileView1.TileTemplate.Add(toolbox);
                tileView1.TileTemplate.Add(image);
                //
                leftPanel.StretchVertical = true;
                leftPanel.Width = 150;
                leftPanel.TextLocation = new Point(-10, 0);
                leftPanel.Appearance.Normal.BackColor = Color.FromArgb(58, 166, 101);
                //
                splitLine.StretchVertical = true;
                splitLine.Width = 3;
                splitLine.TextAlignment = TileItemContentAlignment.Manual;
                splitLine.TextLocation = new Point(140, 0);
                splitLine.Appearance.Normal.BackColor = Color.White;
                //
                idCaption1.Text = "序号";
                idCaption1.TextAlignment = TileItemContentAlignment.TopLeft;
                idCaption1.Appearance.Normal.FontSizeDelta = -1;
                //
                idValue.Column = tileView1.Columns["f_id"];
                idValue.AnchorElement = idCaption1;
                idValue.AnchorIndent = Cap2ValAnchor;
                idValue.MaxWidth = 100;
                idValue.Appearance.Normal.FontStyleDelta = FontStyle.Bold;
                idValue.Appearance.Normal.Font = new Font("Segoe UI Semilight", 16F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
                //
                nameCaption.Text = "工具名称";
                nameCaption.AnchorElement = idValue;
                nameCaption.AnchorIndent = Item2ItemAnchor;
                nameCaption.Appearance.Normal.FontSizeDelta = -1;
                //
                nameValue.Column = tileView1.Columns["f_tooltypename"];
                nameValue.AnchorElement = nameCaption;
                nameValue.AnchorIndent = Cap2ValAnchor;
                nameValue.Appearance.Normal.FontStyleDelta = FontStyle.Bold;
                nameValue.Appearance.Normal.Font = new Font("Segoe UI Semilight", 16F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
                //
                rfidCaption.Text = "电子标签";
                rfidCaption.AnchorElement = nameValue;
                rfidCaption.AnchorIndent = Item2ItemAnchor;
                rfidCaption.Appearance.Normal.FontSizeDelta = -1;
                //
                rfidValue.Column = tileView1.Columns["f_rfid"];
                rfidValue.AnchorElement = rfidCaption;
                rfidValue.AnchorIndent = Cap2ValAnchor;
                rfidValue.Appearance.Normal.FontStyleDelta = FontStyle.Bold;
                rfidValue.Appearance.Normal.Font = new Font("Segoe UI Semilight", 16F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
                //
                toolbox.Column = tileView1.Columns["f_toolboxname"];
                toolbox.TextAlignment = TileItemContentAlignment.BottomLeft;
                toolbox.Appearance.Normal.Font = new Font("Segoe UI Semilight", 16F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
                //
                image.Column = tileView1.Columns["Image"];
                image.ImageSize = new Size(190, 190);
                image.ImageAlignment = TileItemContentAlignment.MiddleRight;
                image.ImageScaleMode = TileItemImageScaleMode.ZoomOutside;
                image.ImageLocation = new Point(10, 0);

            }
            finally
            {
                tileView1.EndUpdate();
            }
        }

        Color colorPanel = Color.FromArgb(158, 158, 158);
        Color colorPanelSel = Color.DarkSeaGreen;
        Color colorCaption = Color.FromArgb(219, 219, 219);
        Color colorCaptionSel = Color.FromArgb(193, 222, 204);

        private void tileView1_ItemCustomize(object sender, TileViewItemCustomizeEventArgs e)
        {
            if (e.Item == null || e.Item.Elements.Count == 0)
                return;

            int id = (int)tileView1.GetRowCellValue(e.RowHandle, tileView1.Columns["f_id"]);
            UpdateItemStatus(e.Item, m_dicToolSelect[id]);
        }

        private void UpdateItemStatus(TileViewItem tileViewItem, bool bSelect)
        {
            Color panelColor = colorPanel;
            Color captionColor = colorCaption;
            if (bSelect)
            {
                panelColor = colorPanelSel;
                captionColor = colorCaptionSel;
            }

            tileViewItem.Elements[0].Appearance.Normal.BackColor = panelColor;
            tileViewItem.Elements[2].Appearance.Normal.ForeColor = captionColor;
            tileViewItem.Elements[4].Appearance.Normal.ForeColor = captionColor;
            tileViewItem.Elements[6].Appearance.Normal.ForeColor = captionColor;
        }

        private void tileView1_ItemClick(object sender, TileViewItemClickEventArgs e)
        {
            var sColumn = tileView1.Columns["f_id"];
            if (sColumn == null)
                return;

            DataRow dataRow = (DataRow)tileView1.GetDataRow(e.Item.RowHandle);
            if (dataRow == null)
                return;

            int id = int.Parse(dataRow["f_id"].ToString());
            m_dicToolSelect[id] = !m_dicToolSelect[id];
            UpdateItemStatus(e.Item, m_dicToolSelect[id]);
        }

        private void windowsUIButtonPanel1_ButtonClick(object sender, DevExpress.XtraBars.Docking2010.ButtonEventArgs e)
        {
            if (e.Button.Properties.Caption == "返回")
            {
                this.DialogResult = DialogResult.OK;
            }
            else if (e.Button.Properties.Caption == "清空所选")
            {
                InitData();
            }
            else if (e.Button.Properties.Caption == "确认提交")
            {
                ApplyOrder();
            }
        }

        // 提交工单
        private void ApplyOrder()
        {
            List<int> listToolID = new List<int>();

            foreach (KeyValuePair<int, bool> kvp in m_dicToolSelect)
            {
                if (kvp.Value.Equals(true))
                {
                    listToolID.Add(kvp.Key);
                }
            }

            // 修改工具状态
            if (!DBManager.GetInstance().ApplyTools(listToolID))
            {
                MessageBox.Show("修改工具状态失败");
                return;
            }

            // 提交订单
            if (!DBManager.GetInstance().ApplyOrder(DataCache.GetInstance().OUserInfo.SUserCode, DateTime.Now, listToolID))
            {
                MessageBox.Show("提交工单失败!");
                return;
            }

            MessageBox.Show("提交工单成功!");
            this.DialogResult = DialogResult.OK;
        }
    }
}